import json

from PyQt5.QtWidgets import (
    QHBoxLayout,
    QVBoxLayout,
    QLabel,
    QLineEdit,
    QPushButton,
    QWidget,
    QFileDialog,
    QComboBox,
    QMessageBox,
)
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from pydantic import BaseModel, Field, field_validator
from ..basic_grid_component import BasicGridComponent

# import gui.controls.envs as envs
from ..dlg_model_params import DlgModelParams
from ..utils import Utils
from ..util import util_key_map


from enum import Enum


class FloatEnumWithAlias(float, Enum):
    def __new__(cls, value, alias):
        obj = float.__new__(cls, value)
        obj._value_ = value
        obj.alias = alias
        return obj


# class LoadAmplitudeEnum(FloatEnumWithAlias):
#     t1 = 1, "1倍"
#     t2 = 1.5, "1.5倍"
#     t3 = 2, "2倍"


# 定义 Pydantic 模型
class LoadBoundaryData(BaseModel):
    loadCondition: str = Field(
        description="载荷工况",
        alias="loadCondition",
        required=True,
    )
    loadAmplitude: float = Field(
        description="载荷幅值",
        alias="loadAmplitude",
        required=True,
    )


class LoadBoundaryComponent(BasicGridComponent):
    detailParams_modelShow_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)

    def get_model(self):
        return LoadBoundaryData

    def updateUi(self, data):
        data = util_key_map.convertKey(data, 0)
        for k, v in data.items():
            if isinstance(self.gridWidgets[k], QComboBox):
                # self.gridWidgets[k].setCurrentText(v)
                for index in range(self.gridWidgets[k].count()):
                    if self.gridWidgets[k].itemData(index) == v:
                        self.gridWidgets[k].setCurrentIndex(index)
                        break
            else:
                self.gridWidgets[k].setValue(v)


class DlgLoadBoundary(DlgModelParams):
    loadBoundary_save_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super(DlgLoadBoundary, self).__init__(parent)
        self.setWindowTitle("载荷与边界条件")
        self.component = LoadBoundaryComponent()
        self.detailParams_modelShow_signal = (
            self.component.detailParams_modelShow_signal
        )
        self.vbox.insertWidget(0, self.component)
        # data = Utils.read_json("data/ipimas/uis/LoadBoundary.json")
        # self.updateUi(data)

    def init(self, data):
        print("1=", data)
        self.updateUi(data)

    @pyqtSlot()
    def on_pBtnOK_clicked(self):
        valueRes = self.component.getValues()
        # descriptionRes = self.component.getValuesUseDescription()
        file = "data/ipimas/uis/LoadBoundary.json"
        data = util_key_map.convertKey(valueRes, 1)
        Utils.write_json(file, data)
        self.close()
        # if res:
        #     self.close()
        #     if 'loadAmplitude' in res:
        #         res['loadAmplitude'] = res['loadAmplitude'].value
        self.loadBoundary_save_signal.emit(data)

    def closeEvent(self, event):
        DlgLoadBoundary._shown = False  # 使用类名访问类属性
        DlgLoadBoundary._instance = None  # 清除实例引用
        event.accept()
